//
//  COHomeHTTP.swift
//  CocoProject
//
//  Created by Ruthless on 2023/8/28.
//

import UIKit
import Moya

let homeProvider = MoyaProvider<COHomeHTTP>()
enum COHomeHTTP {
    case square(page: Int)
    case qa(page: Int, type: Int)
    case qaDetail(id: Int)
    case remark(page: Int, areaId: Int)
    case answer(areaId: Int, comment: String)
    case food
    case sqDetail(id: Int)
    case sqRemark(page: Int, id: Int)
    case sqAnswer(id: Int, comment: String)
    case sqLike(id: Int)
    case sqUnlike(id: Int)
    case create(photo: String, content: String, type: Int, category: Int)
    case uploadImage(image: UIImage)
}

extension COHomeHTTP: TargetType {
    var method: Moya.Method {
        switch self {
        case .square:
            return .post
        case .qa:
            return .post
        case .qaDetail:
            return .get
        case .remark:
            return .post
        case .answer:
            return .post
        case .food:
            return .get
        case .sqDetail:
            return .get
        case .sqRemark:
            return .post
        case .sqAnswer:
            return .post
        case .sqLike:
            return .get
        case .sqUnlike:
            return .get
        case .create:
            return .post
        case .uploadImage:
            return .post
        }
    }
    
    var path: String {
        switch self {
        case .square:
            return "/coco/coco589/square/page/002"
        case .qa:
            return "/coco/coco589/square/q0002/page"
        case .qaDetail:
            return "/coco/coco589/square/q0003"
        case .remark:
            return "/coco/coco589/square/q0003/comment/page"
        case .answer:
            return "/coco/coco589/square/q0003/comment"
        case .food:
            return "/coco/coco589/square/q0002/type"
        case .sqDetail:
            return "/coco/coco589/square/003"
        case .sqRemark:
            return "/coco/coco589/square/comment/page"
        case .sqAnswer:
            return "/coco/coco589/square/comment"
        case .sqLike:
            return "/coco/coco589/square/like"
        case .sqUnlike:
            return "/coco/coco589/square/nuLike"
        case .create:
            return "/coco/coco589/square/create/001"
        case .uploadImage:
            return "/coco/coco589/user/9084/upload"
        }
    }
    
    var task: Moya.Task {
        var params: [String: Any] = [:]
        switch self {
        case let .square(page):
            params["pagSize"] = 20
            params["pagNum"] = page
        case let .qa(page, type):
            params["pagSize"] = 20
            params["pagNum"] = page
            params["type"] = type
        case let .qaDetail(id):
            params["id"] = id
            return .requestParameters(parameters: params, encoding: URLEncoding.default)
        case let .remark(page, areaId):
            params["pagSize"] = 20
            params["pagNum"] = page
            params["areaId"] = areaId
        case let .answer(areaId, comment):
            params["areaId"] = areaId
            params["comment"] = comment
        case .food:
            return .requestParameters(parameters: params, encoding: URLEncoding.default)
        case let .sqDetail(id):
            params["id"] = id
            return .requestParameters(parameters: params, encoding: URLEncoding.default)
        case let .sqRemark(page, id):
            params["pagSize"] = 20
            params["pagNum"] = page
            params["squareId"] = id
        case .sqAnswer(id: let id, comment: let comment):
            params["squareId"] = id
            params["comment"] = comment
        case .sqLike(id: let id):
            params["id"] = id
            return .requestParameters(parameters: params, encoding: URLEncoding.default)

        case .sqUnlike(id: let id):
            params["id"] = id
            return .requestParameters(parameters: params, encoding: URLEncoding.default)
        case let .create(photo, content, type, category):
            params["photo"] = photo
            params["content"] = content
            params["type"] = type
            params["category"] = category
        case let .uploadImage(image):
            let imageData = image.pngData()
            let fileName = "\(Date().timeIntervalSince1970).png"
            let formData = MultipartFormData(provider: .data(imageData!), name: "file", fileName: fileName, mimeType: "image/png")
            return .uploadMultipart([formData])
        }
        
        return .requestParameters(parameters: params, encoding: JSONEncoding.default)
    }
    
    var headers: [String : String]? {
        var params: [String: String] = [:]
        params["AVS"] = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? ""
        params["OSB"] = "1"
        params["AGKB"] = "H1"
        params["token"] = COUserManager.userInfo?.token
        return params
    }
}
